package com.base.control.base;


import com.base.common.constant.RedisKey;
import com.base.control.model.DictionariesDto;
import com.base.domain.module.vo.DictionariesVo;
import com.base.domain.service.IDictionariesService;
import com.redis.sdk.cachecore.CacheLocal;
import com.redis.sdk.cachecore.CacheLocalDelete;
import com.redis.sdk.cachecore.CacheRedis;
import com.redis.sdk.cachecore.CacheRedisDelete;
import com.redis.sdk.lock.LockRedis;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cyj
 * @since 2021-12-24
 */
@RestController
@RequestMapping("/dictionaries")
@Slf4j
@Api(tags = "字典管理")
public class DictionariesController {

    @Autowired
    private IDictionariesService dictionariesService;

    @GetMapping("/list")
    @ApiOperation("查询字典")
    @CacheRedis(prefix = RedisKey.DICTIONARIES, value = "#group")
    @CacheLocal(value = "#group")
    @LockRedis(value = {"group-", "#group"})
    public List<DictionariesVo> group(@RequestParam("group") String group) {
        return dictionariesService.group(group);
    }

    @PostMapping("/add")
    @ApiOperation("字典新增")
    @CacheRedisDelete(prefix = RedisKey.DICTIONARIES, value = "#dictionariesDto.fgroup")
    @CacheLocalDelete(value = "#dictionariesDto.fgroup")
    public void add(@RequestBody DictionariesDto dictionariesDto) {
        dictionariesService.add(dictionariesDto);
    }

    @PutMapping("/update")
    @ApiOperation("字典更新")
    @CacheRedisDelete(prefix = RedisKey.DICTIONARIES, value = "#dictionariesDto.fgroup")
    @CacheLocalDelete(value = "#dictionariesDto.fgroup")
    public void update(@RequestBody DictionariesDto dictionariesDto) {
        dictionariesService.update(dictionariesDto);
    }

    @DeleteMapping("/delete")
    @ApiOperation("字典删除")
    @CacheRedisDelete(prefix = RedisKey.DICTIONARIES, value = "#group")
    @CacheLocalDelete(value = "#group")
    public void delete(@RequestParam("id") Integer id, @RequestParam("group") String group) {
        dictionariesService.delete(id);
    }

}

